package com.example.everying.controller.content;

import com.example.everying.annotation.WebLog;
import com.example.everying.context.UserContext;
import com.example.everying.model.vo.content.ActivityArticleVo;
import com.example.everying.service.content.ActivityArticleService;
import com.example.everying.service.content.ActivityThumbService;
import com.example.everying.utils.Result;
import com.example.everying.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: everything
 * @description: 文章点赞管理层
 * @author: zw
 * @create: 2025-05-22 15:15
 **/
@RestController
@Slf4j
@RequestMapping("/activity/thumb")
public class ActivityThumbController {

    @Autowired
    private ActivityArticleService articleService;

    @Autowired
    private ActivityThumbService thumbService;


    /**
     * 点赞文章
     */
    @GetMapping("/{articleId}")
    @WebLog(description = "点赞文章")
    public Result<?> likeArticle(@PathVariable String articleId) {
        boolean result = thumbService.likeArticle(articleId, UserContext.getUser().getId());
        return result ? Result.ok() : Result.fail("点赞失败");
    }

    /**
     * 取消点赞文章
     */
    @DeleteMapping("/{articleId}")
    @WebLog(description = "取消点赞文章")
    public Result<?> unlikeArticle(@PathVariable String articleId) {
        boolean result = thumbService.unlikeArticle(articleId,UserContext.getUser().getId());
        return result ? Result.ok() : Result.fail("取消点赞失败");
    }

    /**
     * 检查用户是否已点赞文章
     */
    @GetMapping("/{articleId}/hasLiked")
    public Result<Boolean> hasLiked(@PathVariable String articleId) {
        boolean result = thumbService.hasLiked(articleId,UserContext.getUser().getId());
        return Result.ok(result);
    }

    /**
     * 获取文章点赞数
     */
    @GetMapping("/{articleId}/count")
    public Result<Long> getLikesCount(@PathVariable String articleId) {
        Long  count= thumbService.getLikesCount(articleId);
        return Result.ok(count);
    }

    /**
     * 获取文章的点赞用户ID集合
     */
//    @GetMapping("/{articleId}/users")
//    public Result<Set<String>> getArticleLikes(@PathVariable String articleId) {
//        Set<String> userIds = thumbService.getArticleLikes(articleId);
//        return Result.ok(userIds);
//    }
    /**
     * 用户点赞列表
     */
    @PostMapping("/getUserLikes")
    public Result<List<ActivityArticleVo>> getUserLikes(String userId) {
        String list = StringUtils.isEmpty(userId) ? UserContext.getUser().getId() : userId;
        List<ActivityArticleVo> userLikes = thumbService.getUserLikes(list);
        return Result.success(userLikes);
    }

    /**
     * 批量检查用户是否已点赞文章
     */
    @PostMapping("/batch/hasLiked")
    public Result<Map<String, Boolean>> batchHasLiked(@RequestBody List<String> articleIds) {
        Map<String, Boolean> result = thumbService.batchHasLiked(articleIds, UserContext.getUser().getId());
        return Result.ok(result);
    }

    /**
     * 批量获取文章点赞数
     */
    @PostMapping("/batch/count")
    public Result<Map<String, Long>> batchGetLikesCount(@RequestBody List<String> articleIds) {
        Map<String, Long> result = thumbService.batchGetLikesCount(articleIds);
        return Result.ok(result);
    }

    /**
     * 批量获取文章点赞信息（状态+数量）
     */
    @PostMapping("/batch/info")
    public Result<Map<String, Object>> batchGetLikeInfo(@RequestBody List<String> articleIds) {
        String userId = UserContext.getUser().getId();
        Map<String, Boolean> likeStatus = thumbService.batchHasLiked(articleIds, userId);
        Map<String, Long> likeCount = thumbService.batchGetLikesCount(articleIds);
        
        Map<String, Object> result = new HashMap<>();
        result.put("status", likeStatus);
        result.put("count", likeCount);
        
        return Result.ok(result);
    }
    @PostMapping("/getMessageList")
    public Result<List<Map<String, Object>>> getMessageList(@RequestBody Map<String, Object> params) {
        String userId = UserContext.getUser().getId();
        Integer page = (Integer) params.getOrDefault("page", 1);
        Integer limit = (Integer) params.getOrDefault("limit", 20);
        
        List<Map<String, Object>> messages = thumbService.getMessageList(userId, page, limit);
        return Result.ok(messages);
    }

    // /**
    //  * 获取未读消息数
    //  */
    // @GetMapping("/getUnreadCount")
    // public Result<Integer> getUnreadCount() {
    //     String userId = UserContext.getUser().getId();
    //     Integer count = thumbService.getUnreadCount(userId);
    //     return Result.ok(count);
    // }

    // /**
    //  * 标记消息为已读
    //  */
    // @PostMapping("/markAsRead/{messageId}")
    // public Result<?> markAsRead(@PathVariable String messageId) {
    //     String userId = UserContext.getUser().getId();
    //     thumbService.markAsRead(messageId, userId);
    //     return Result.ok();
    // }

    // /**
    //  * 标记所有消息为已读
    //  */
    // @PostMapping("/markAllAsRead")
    // public Result<?> markAllAsRead() {
    //     String userId = UserContext.getUser().getId();
    //     thumbService.markAllAsRead(userId);
    //     return Result.ok();
    // }

}
